package cn.edu.nuc.onlinestore.frame;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import cn.edu.nuc.onlinestore.util.GoodsUpdateutil;
import cn.edu.nuc.onlinestore.vo.Goods;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JTextArea;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.ImageIcon;

public class AdminUpdate extends JFrame {

	private JPanel contentPane;
	private JTextField upname;
	private JTextField upprice;
	private JTextField upnum;

	/**
	 * Launch the application.
//	 */
//	public static void main(String[] args) {
//		EventQueue.invokeLater(new Runnable() {
//			public void run() {
//				try {
//					AdminUpdate frame = new AdminUpdate();
//					frame.setVisible(true);
//				} catch (Exception e) {
//					e.printStackTrace();
//				}
//			}
//		});
//	}

	/**
	 * Create the frame.
	 */
	public AdminUpdate(Goods g) {
		
		setTitle("修改商品");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 405);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel label = new JLabel("商品名称:");
		label.setBounds(23, 57, 66, 15);
		contentPane.add(label);
		
		upname = new JTextField();
		upname.setBounds(99, 54, 269, 21);
		contentPane.add(upname);
		upname.setColumns(10);
		
		JLabel label_1 = new JLabel("单价:");
		label_1.setBounds(35, 100, 54, 15);
		contentPane.add(label_1);
		
		upprice = new JTextField();
		upprice.setBounds(99, 97, 212, 21);
		contentPane.add(upprice);
		upprice.setColumns(10);
		
		JLabel label_2 = new JLabel("单位:元");
		label_2.setBounds(321, 100, 54, 15);
		contentPane.add(label_2);
		
		JLabel label_3 = new JLabel("数量:");
		label_3.setBounds(35, 143, 54, 15);
		contentPane.add(label_3);
		
		upnum = new JTextField();
		upnum.setBounds(99, 140, 212, 21);
		contentPane.add(upnum);
		upnum.setColumns(10);
		
		JLabel label_4 = new JLabel("简介:");
		label_4.setBounds(35, 188, 54, 15);
		contentPane.add(label_4);
		
		JTextArea uptext = new JTextArea();
		uptext.setBounds(99, 184, 269, 103);
		contentPane.add(uptext);
		
		JButton button = new JButton("确定修改");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				 String name = null;
				double price = 0;
				int num = 0;
				String info = null;
				boolean t;
				try {
					name = upname.getText();
					 price = new Double(upprice.getText());
					 num = new Integer(upnum.getText());
					 info = uptext.getText();
					 System.out.println(name);
					 System.out.println(price);
					 System.out.println(num);
					 System.out.println(info);
				} catch (NumberFormatException e1) {
					// TODO Auto-generated catch block
					JOptionPane.showMessageDialog(null, "输入了错误的类型", "alert", JOptionPane.ERROR_MESSAGE); 
				}
				
				GoodsUpdateutil gu=new GoodsUpdateutil();
				t=gu.addGoods(name,price,num,info);
				if(t){
					JOptionPane.showMessageDialog(null, "添加成功", "alert", JOptionPane.ERROR_MESSAGE);
				}
			}
		});
		button.setBounds(275, 310, 93, 23);
		contentPane.add(button);
		
		JLabel label_5 = new JLabel(">=0");
		label_5.setBounds(321, 143, 43, 15);
		contentPane.add(label_5);
		
		upname.setText(g.getName());
		upprice.setText(String.valueOf(g.getPrice()));
		upnum.setText(String.valueOf(g.getNum()));
		uptext.setText(g.getInfo());
		
		JLabel lblNewLabel = new JLabel("New label");
		lblNewLabel.setIcon(new ImageIcon(AdminUpdate.class.getResource("/image/4.png")));
		lblNewLabel.setBounds(0, 0, 434, 367);
		contentPane.add(lblNewLabel);
		//String.valueOf(g.getId()),g.getName(),String.valueOf(g.getPrice()),String.valueOf(g.getNum())
		this.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				// TODO Auto-generated method stub
				((AdminUpdate)e.getSource()).setVisible(false);
			}
		});
	}
}
